<?php
/**
 * @ClassName: Goods
 * @Description: TODO
 * @author: gydtrade
 * @Date: 2025-10-13 14:26
 * @Version: V1.0.0
 */

namespace app\api\controller;

use app\common\logic\GoodsLogic;
use app\common\model;

class Goods extends Api
{
    protected $noNeedLogin = ['detail', 'goodsList'];

    /**
     * 获取商品分类
     * @return \think\response\Json
     */
    function getProductCategory()
    {
        $category_id = input('category_id', 0);
        $parent_id = input('parent_id', 0);
        $skip = input('skip', 0);
        $limit = input('limit', 20);

        $where = [];
        $where[] = ['is_show', 'eq', 1];

        if ($category_id) {
            $where[] = ['id', 'eq', $category_id];
        }
        if ($parent_id) {
            $where[] = ['parent_id', 'eq', $parent_id];
        }
        $list = model\ShopCategory::where($where)->field('id,name,subtitle,flag')->order('sort asc')->limit($skip, $limit)->select();

        $result = [
            'list' => $list,
            'is_more' => count($list) >= $limit,
        ];

        return json(["code" => 1, "msg" => "OK", "data" => $result]);
    }

    /**
     * 商品详情
     * @return \think\response\Json
     */
    function detail()
    {
        $goods_id = input('goods_id', 0);

        $data = model\ShopGoods::get($goods_id);

        $result = [];
        if ($data) {
            $result = (new GoodsLogic())->dataConversion($data);
        }

        return json(['code' => 1, 'data' => $result]);
    }

    /**
     * 商品列表
     * @return \think\response\Json
     */
    function productList()
    {
        $keyword = input('keyword', '');
        $delivery_current = input('delivery_current', 0);//0配送，1自提
        $store_id = input('store_id', 0);
        $category_id = input('category_id', 0);
        $category_flag = input('category_flag', '');
        $brand_id = input('brand_id', 0);
        $is_free_queuing = input('is_free_queuing', 0);//0正价购买，1排队免单
        $skip = input('skip', 0);
        $limit = input('limit', 10);

        $where = [];
        $where[] = ["on_sale", "=", 1];
        $where[] = ['is_show', '=', 1];
        $where[] = ['is_free_queuing', '=', $is_free_queuing];
        $where[] = ['goods_type', 'in', [4, 5, 6]];//商品类型，0普通商品，1积分商品，2酒店，3门票，4水票，5饮水设备，6水产品，7虚拟商品

        if ($delivery_current == 1) {
            $where[] = ['delivery_type', 'in', [2, 3]];
        } else {
            $where[] = ['delivery_type', 'in', [1, 3]];
        }
        if ($keyword) {
            $where[] = ['name|keyword|description', 'like', '%' . $keyword . '%'];
        }
        if ($store_id) {
            $where[] = ['store_id', '=', $store_id];
        }
        if ($category_id) {
            $where[] = ['category_id', '=', $category_id];
        }
        if ($brand_id) {
            $where[] = ['brand_id', '=', $brand_id];
        }

        $order = "is_top desc,is_recommend desc,is_hot desc,sort";
//        var_dump(model\ShopGoods::where($where)->order($order)->limit($skip, $limit)->fetchSql()->select());
        $goods_list = model\ShopGoods::where($where)->order($order)->limit($skip, $limit)->select();

        foreach ($goods_list as &$goods) {
            $goods = (new GoodsLogic())->dataConversion($goods);
        }

        $result = [
            'list' => $goods_list,
            'is_more' => count($goods_list) >= $limit,
        ];

        return json(['code' => 1, 'data' => $result]);
    }

    /**
     * 商品列表
     * @return \think\response\Json
     */
    function goodsList()
    {
        //商品类型，0普通商品，1积分商品，2酒店，3门票，4水票，5水架设备，6水产品，7虚拟商品
        $goods_type = input('goods_type', 0);
        $category_id = input('category_id', 0);
        $skip = input('skip', 0);
        $limit = input('limit', 10);

        $where = [];
        $where[] = ["on_sale", "=", 1];
        $where[] = ['is_show', '=', 1];
        $where[] = ["goods_type", "=", $goods_type];

        if ($category_id) {
            $where[] = ["category_id", "=", $category_id];
        }

        switch ($goods_type) {
            case 1:
                // 积分商品
                $where[] = ["exchange_type", "<>", 0];//兑换类型，0无，1积分，2积分+现金
                $where[] = ['points', '>', 0];//兑换积分
                break;
            case 2:
                // 酒店
                break;
            case 3:
                // 门票
                break;
            case 4:
                // 水票
                break;
            case 5:
                // 水架设备
                break;
            case 6:
                // 水产品
                break;
            case 7:
                // 虚拟商品
                break;
            default:
                // 普通商品
                break;
        }

        $order = "is_top desc,is_recommend desc,is_hot desc,sort";
//        var_dump(model\ShopGoods::where($where)->order($order)->limit($skip, $limit)->fetchSql()->select());
        $goods_list = model\ShopGoods::where($where)->order($order)->limit($skip, $limit)->select();

        foreach ($goods_list as &$goods) {
            $goods = (new GoodsLogic())->dataConversion($goods);
        }

        $result = [
            'list' => $goods_list,
            'is_more' => count($goods_list) >= $limit,
        ];

        return json(['code' => 1, 'data' => $result]);
    }

    /**
     * 删除商品
     * @return \think\response\Json
     */
    function del()
    {
        $goods_id = input('goods_id', 0);

        $res = model\ShopGoods::where('id', $goods_id)->update([
            'on_sale' => 0,
            'is_show' => 0,
            'delete_time' => date_now(),
            'update_time' => date_now(),
        ]);

        if (!$res) {
            return json(['code' => 0, 'msg' => '删除失败']);
        }

        return json(['code' => 1, 'msg' => '删除成功']);
    }

    /**
     * 设置上下架状态
     * @return \think\response\Json
     */
    function setSaleStatus()
    {
        $id = input('id', 0);
        $on_sale = input('on_sale', 0);

        $goods = model\ShopGoods::get($id);
        if (!$goods) {
            return json(['code' => 0, 'msg' => '商品异常']);
        }

        if ($goods['status'] != 1) {
            return json(['code' => 0, 'msg' => '商品未审核通过，不允许操作']);
        }

        if ($on_sale == 1) {
            $msg = '上架';
        } else {
            $msg = '下架';
        }

        $res = model\ShopGoods::where('id', $id)->update([
            'on_sale' => $on_sale,
            'update_time' => date_now(),
        ]);

        if (!$res) {
            return json(['code' => 0, 'msg' => $msg . '失败']);
        }

        return json(['code' => 1, 'msg' => $msg . '成功']);
    }

}